//
//  QCHttpHandler.m
//  News
//
//  Created by QC.L on 15/1/16.
//  Copyright (c) 2015年 QC.L. All rights reserved.
//

#import "QCHttpHandler.h"
#import <AFNetworking.h>

@implementation QCHttpHandler

+ (void)get:(NSString *)url body:(NSDictionary *)body HTTPHead:(NSDictionary *)httpHead result:(QCHTTPStyle)QCHTTPStyle success:(void (^)(id result))success failure:(void (^)(NSError *error))failure
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    // 2.判断网络数据形式
    switch (QCHTTPStyle) {
        case QCHttp:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case QCJson:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case QCXml:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain",nil]];

    // 3.将url进行文字处理
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    if (httpHead) {
        for (NSString *key in httpHead) {
            [manager.requestSerializer setValue:[httpHead objectForKey:key] forHTTPHeaderField:key];
        }
    }
    
    
    // 4.发送GET请求
    [manager GET:url parameters:body
     success:^(AFHTTPRequestOperation *operation, id responseObj) {
         if (success) {
            success(responseObj);
         }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         if (failure) {
             failure(error);
         }
     }];
}


+ (void)post:(NSString *)url body:(id)body HTTPHead:(NSDictionary *)httpHead requestStyle:(QCBodyStyle)requestStyle  result:(QCHTTPStyle)QCHTTPStyle success:(void (^)(id))success failure:(void (^)(NSError *))failure;
{
    // 1.获得请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // 2.上传数据时body的类型
    switch (requestStyle) {
        case NSStringStyle:
            // 会将字典转换成字符串
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
        case JSONStyle:
            // 保持JSON格式
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case NomalStyle:
            break;
        default:
            break;
    }
    if (httpHead) {
        for (NSString *key in httpHead) {
            [manager.requestSerializer setValue:[httpHead objectForKey:key] forHTTPHeaderField:key];
        }
    }
    
    // 3.判断网络数据形式
    switch (QCHTTPStyle) {
        case QCHttp:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case QCJson:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case QCXml:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    // 3.发送POST请求
    [manager POST:url parameters:body
      success:^(AFHTTPRequestOperation *operation, id responseObj) {
          if (success) {
              success(responseObj);
          }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (failure) {
              failure(error);
          }
      }];
}

@end